package httplib;

/**
 * Created by niraymond on 16/8/18.
 */

import java.io.Serializable;
import java.text.DecimalFormat;
import java.util.Date;

public abstract class BaseModel implements Serializable{

    protected String safeGet(String v){
        return v;
    }

    protected int safeGet(Integer v){
        return v;
    }

    protected double safeGet(Double v){
        if(v == null)
            return 0d;
        return v;
    }

    protected float safeGet(Float v){
        if(v == null)
            return 0f;
        return v;
    }

    public String getData(String s){
        return "unknow filed";
    }
    private String mFlag = "";
    public String getData(String s,String flag){
        mFlag = flag;
        return getData(s);
    }
    private String getBindDataFlag(String path){
        int splitLen = path.indexOf("#");
        if(splitLen == -1){
            return "";
        }

        if(splitLen+1 > path.length()){
            return "";
        }
        return path.substring(splitLen+1,path.length());
    }

    private String getBindDataPath(String path){
        int splitLen = path.indexOf("#");
        if(splitLen == -1){
            return path;
        }

        return path.substring(0, splitLen);
    }


    protected String safeGetDtoData(Object data,String path){
        if(data == null)
            return "";
        return data.toString();
    }

    protected String safeGetDtoData(Number data,String path){
        if(data == null)
            return "";
        return data.toString();
    }
    protected String  safeGetDtoData(Integer data,String path){
        if(data == null)
            return "";

        return data.toString();
    }
    protected String  safeGetDtoData(Double data,String path){
        if(data == null)
            return "";

        if(mFlag.contains("math_Amount")){
            DecimalFormat df = new DecimalFormat("0.00");
            return df.format(data);//+"math_Amount";
        }else if(mFlag.contains("math_Qty")){
            return formatDecimalAsQty(data);//+"math_Qty";
        }
        return data.toString();
    }

    protected String safeGetDtoData(Date data,String path){
        if(data == null)
            return "";
        String dataFlag = getBindDataFlag(path);
//		String dataPath = getBindDataPath(path);
        if(dataFlag.isEmpty()){
            return data.toString();
        }
        return "";
    }

    protected String safeGetDtoData(BaseModel data,String path){
        if(data == null){
            return "";
        }else{
            String[] ps = path.split(".");
            if(ps!=null && ps.length >1){
                path = path.replaceFirst(ps[0] + ".", "");
                return data.getData(path);
            }else{
                return data.toString();
            }
        }
    }

    public String formatDecimalAsQty(double qty){
        int real = (int) qty;
        if(qty-real>0.001){
            DecimalFormat df = new DecimalFormat("0.0");
            return df.format(qty);
        }else{
            return String.valueOf(real);
        }
    }
}
